package thread;

/**
 * Created with IntelliJ IDEA.
 * Description:中断线程，方法2-使用Thread方法
 * User: bjc
 * Date: 2025-08-15
 * Time: 19:39
 */
public class ThreadDemo11 {
    public static void main(String[] args) {
        Thread t = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("我是一个线程，正在工作中...");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
//                    break;
                    //线程被中断，会抛出异常，这里会捕获异常，并设置中断标志位为true
                    Thread.currentThread().interrupt();
                }
            }
            System.out.println("线程执行完毕！");
        });
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("让t线程结束");
        t.interrupt();//将线程状态修改为true
    }
}
